package com.campos.collection;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;


import com.campos.common.ContextData;
import com.campos.common.Registry;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Queue;

import static androidx.core.app.ActivityCompat.startActivityForResult;

public class GPSServiceWrapper {

    private static GPSServiceWrapper __ginstance = null;
    public static GPSServiceWrapper getInstance(){
        if(__ginstance == null){
            __ginstance = new GPSServiceWrapper();
        }
        return __ginstance;
    }

    boolean mBound = false;
    private ServiceConnection mConnection = null;

    public boolean getServiceStatus(){
        return mConnection != null;
    }


    public boolean startService(){
        if(mConnection != null) return true;
        mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                gpsListener = new GPSListener();
                GPSService.getInstance().addGPSListener(gpsListener);
                mBound = true;
            }
            public void onServiceDisconnected(ComponentName className) {
                gpsListener = null;
                mBound = false;
            }
        };
        Context c = Registry.getInstance().getApplicationContext();
        if(c != null){
            boolean res =c.bindService(new Intent(c, GPSService.class), mConnection, Context.BIND_AUTO_CREATE);
            if(!res){
                mConnection = null;
            }
            return res;
        }
        return false;
    }

    public boolean stopService(){
        Context c = Registry.getInstance().getApplicationContext();
        if(c != null && mConnection != null){
            c.unbindService(mConnection);
        }
        gpsListener = null;
        mBound = false;
        mConnection = null;
        return true;
    }

    public class LocJson {
        public Long time;
        public double lon;
        public double lat;
        //public double hei;
    }

    private GPSListener gpsListener = null;
    private class GPSListener implements LocationListener {

        GPSListener(){}

        public Location currentLoc = null;

        @Override
        public void onLocationChanged(Location l) {//定位改变监听
            currentLoc = new Location(l);
            ContextData.instance.gpsDataList.add(currentLoc);
        }



        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {//定位状态监听

        }

        @Override
        public void onProviderEnabled(String provider) {//定位状态可用监听

        }

        @Override
        public void onProviderDisabled(String provider) {//定位状态不可用监听

        }
    }

    public Location getCurrentGPS(){
        if(gpsListener == null){
            return null;
        }
        return gpsListener.currentLoc;
    }
}
